package com.eyedog.video.videolist;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;

import com.eyedog.uilibrary.widgets.MyGridLayoutManager;
import com.eyedog.video.R;
import com.eyedog.video.entity.CategoryEntity;
import com.eyedog.video.http.ICallback;
import com.eyedog.video.search.CategoryAdapter;
import com.eyedog.video.search.SearchModel;
import com.eyedog.video.widgets.BasePopupWindow;

import java.util.List;

/**
 * Created by Android Studio.
 * User: wilbert jw20082009@qq.com
 * Date: 2019/7/25 9:46
 */
public class SearchWindow {

    BasePopupWindow window;

    SearchModel searchModel;

    RecyclerView category;

    CategoryAdapter categoryAdapter;

    public SearchWindow(final Context context) {
        searchModel = new SearchModel();
        window = BasePopupWindow.builder(context)
                .contentView(LayoutInflater.from(context).inflate(R.layout.layout_window_search, null, false))
                .isOutsideTouch(true).isFocus(false).isHeightWrap(true).setAlpha(1f).isWidthWrap(false).setOnBasePopupWindowInitListener(new BasePopupWindow.OnBasePopupWindowInitListener() {
                    @Override
                    public void onPopupInitViewListener(View contentView, PopupWindow popupWindow) {
                        category = (RecyclerView) contentView.findViewById(R.id.rv_category);
                        category.setLayoutManager(new MyGridLayoutManager(context, 2));
                        categoryAdapter = new CategoryAdapter(context);
                        category.setAdapter(categoryAdapter);
                        searchModel.loadSearchCategorys(new ICallback<List<CategoryEntity>>() {
                            @Override
                            public void onLoadSuccess(List<CategoryEntity> categoryEntities) {
                                showSearchCategorys(categoryEntities);
                            }

                            @Override
                            public void onLoadFailed(String err) {
                            }
                        });
                    }
                }).build();
    }

    public BasePopupWindow window() {
        return window;
    }

    public void showSearchCategorys(final List<CategoryEntity> categoryEntities) {
        category.post(new Runnable() {
            @Override
            public void run() {
                category.setVisibility(View.VISIBLE);
                categoryAdapter.cleanData();
                categoryAdapter.add(categoryEntities);
            }
        });
    }
}
